package cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.script;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.AiNodeScriptParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.AiNodeScriptParamOut;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exception.AutoDesignException;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.AiTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.AbstractAiSendSendTool;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.handler.Kimi8kAiHandlerSend;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.handler.TransAiHandlerSend;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.NodeTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.streamBus.IStreamSubscriber;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.utils.AiJsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: cong.pu
 * @create: 2024-10-14 10:35
 **/
@Slf4j
public class DefaultAiScript extends AbstractAiScript {


    public DefaultAiScript() {

    }

    public DefaultAiScript(String appId, String appKey) {
        TransAiHandlerSend transAiHandlerSend = new TransAiHandlerSend(appId, appKey);
        initAiTool(transAiHandlerSend);
    }

    @Override
    public String getScript(String nodeName, String desc, List<AiNodeScriptParamIn> ins, List<AiNodeScriptParamOut> outs, String nodeType) {
        AbstractAiSendSendTool aiSendSendTool = getAiSendSendTool();
        if (aiSendSendTool == null) {
            throw new AutoDesignException("aiSendSendTool is null");
        }
        AiTypeEnum aiType = AiTypeEnum.生成js脚本;
        if (nodeType.equals(NodeTypeEnum.JS_SCRIPT.getKey())) {
            aiType = AiTypeEnum.生成js脚本;
        } else if (nodeType.equals(NodeTypeEnum.JAVA_SCRIPT.getKey())) {
            aiType = AiTypeEnum.生成java脚本;
        } else if (nodeType.equals(NodeTypeEnum.PYTHON_SCRIPT.getKey())) {
            aiType = AiTypeEnum.生成python脚本;
        }
        String promptFromJsonFile = AiJsonUtils.getPromptFromJsonFile(aiType);

        String question = " 节点名称：\n" +
                "                {name}\n" +
                "                                \n" +
                "                节点描述：\n" +
                "                {desc}\n" +
                "                                \n" +
                "                输入参数：\n" +
                "                {ins}\n" +
                "                                \n" +
                "                输出参数：\n" +
                "                {outs}";

        question = question.replace("{name}", nodeName);
        question = question.replace("{desc}", desc);

        String insStr = getIns(ins);
        String outsStr = getOuts(outs);
        question = question.replace("{ins}", insStr);
        question = question.replace("{outs}", outsStr);

        log.info("promptFromJsonFile:{}", promptFromJsonFile);
        log.info("question:{}", question);
        aiSendSendTool.initSystem(promptFromJsonFile);
        String s = aiSendSendTool.sendToAi(question);
        s = s.replaceAll("```javascript", "");
        s = s.replaceAll("```java", "");
        s = s.replaceAll("```python", "");
        s = s.replaceAll("```", "");
        return s;
    }


    @Override
    public IStreamSubscriber getScriptStream(String nodeName, String desc, List<AiNodeScriptParamIn> ins, List<AiNodeScriptParamOut> outs, String nodeType) {
        return null;
    }

    public static void main(String[] args) {
        DefaultAiScript defaultAiScript = new DefaultAiScript();
//        AbstractAiSendSendTool aiSendSendTool = new TransAiHandlerSend("xxx", "xxx");
        AbstractAiSendSendTool aiSendSendTool = new Kimi8kAiHandlerSend("xxxx");
        defaultAiScript.initAiTool(aiSendSendTool);
        List<AiNodeScriptParamIn> ins = new ArrayList<>();
        AiNodeScriptParamIn a = new AiNodeScriptParamIn();
        a.setParamCode("tesss");
        a.setDesc("");
        a.setDateType(DataTypeEnum.NUMBER);
        ins.add(a);

        List<AiNodeScriptParamOut> outs = new ArrayList<>();
        AiNodeScriptParamOut c = new AiNodeScriptParamOut();
        c.setParamCode("result");
        c.setDesc("");
        c.setDateType(DataTypeEnum.NUMBER);
        outs.add(c);

        String s = defaultAiScript.getScript("js脚本节点", "如果tesss大于1，则输出1，否则输出0", ins, outs, NodeTypeEnum.JS_SCRIPT.getKey());
        System.out.println(s);
    }
}
